package de.hfu.android;

import de.hfu.helper.DataAccess;
import de.hfu.helper.DataCarrier;
import de.hfu.helper.DataParser;
import de.hfu.model.Dish;
import android.app.Activity;
import android.os.AsyncTask;

public class PostCommentAndRatingTask extends AsyncTask<Void, Void, Void>{
	Activity context;
	
	public PostCommentAndRatingTask(Activity context) {
		this.context = context;
	}

	@Override
	protected Void doInBackground(Void... params) {
		DataCarrier carrier = DataCarrier.getInstance();
		DataParser parser = DataParser.getInstance();
		DataAccess dac = DataAccess.getInstance(context);
		
		Dish dish = carrier.getDish();
		boolean ck = false;

		if (carrier.isChanged()) {						
			dac.updateDish(dish);
			carrier.setChanged(false);						
		}

		if (!dish.isPostedComment()) {
			dish.setPostedComment(parser.postCommentToGAE(dish.getTitle(), dish.getDate(), dish.getComment()));
			ck = dish.isPostedComment();
		}
		
		if (!dish.isPostedRating()) {
			dish.setPostedRating(parser.postRatingToGAE(dish.getTitle(), dish.getDate(), dish.getRating()));
			ck = dish.isPostedRating();
		}

		if (ck) 						
			dac.updateDish(dish);

		return null;
	}

}
